package com.yuji.aacar.core.mvvm.appbase;

import android.app.Activity;
import android.support.v4.app.Fragment;

import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.core.mvvm.apptask.TaskToken;
import com.yuji.aacar.net.bean.HttpCoreBaseRequest;
import com.yuji.aacar.net.exception.BaseHttpException;

import java.util.concurrent.atomic.AtomicBoolean;


/**
 * <li>Description: 数据模型绑定基类
 * <br>
 * 每一个activity绑定一个viewmodel，将界面和数据分离， 所有的数据处理和传递都通过activity之间的viewmodel完成。</br>
 *
 * <br>
 * 每个项目中的BaseViewModel都会将所有的数据请求的参数和返回值定义为属性</br>
 * </li>
 */

public abstract class CoreBaseViewModel {

    //viewModelId
    private String viewModelId;

    //activityClassName
    private String activityClassName;

    //requestCode
    private int requestCode;

    //taskToken
    private TaskToken taskToken;

    //是否显示加载框
    public Boolean showProgressBar;

    //当前activity
    private Activity activity;

    //fragment
    private Fragment fragment;

    //返回的数据
    protected CoreServiceResponse response;

    //数据是否准备
    public AtomicBoolean isDataReady = new AtomicBoolean(false);

    //是否已经进行过刷新操作，防止重复操作
    public AtomicBoolean isRefreshed = new AtomicBoolean(false);

    //是否加载失败了
    public AtomicBoolean isFailed = new AtomicBoolean(false);

    //请求参数服务码
    public HttpCoreBaseRequest baseRequest;

    //请求方法名
    public String requestMethodName;

    //异常类型
    public BaseHttpException exception;

    /**
     * 是否是CoreBaseFragment的model对象
     */
    public abstract boolean isFragment();

    public CoreServiceResponse getResponse() {
        return response;
    }

    public Object getRequestResponse() {
        return response.getResponse();
    }

    public synchronized void setResponse(CoreServiceResponse response) {
        this.response = response;
        LogUtil.e("--setResponse--", "["+this.getClass().getSimpleName()+"]" + response);
        isDataReady.set(true);
        doPackData(response.getResponse());
        LogUtil.e("--setResponse--this--", this);
        LogUtil.e("--isFragment--this--", isFragment());
        if(isFragment()) {
            if (null != this.fragment){
                if (fragment instanceof CoreBaseFragment) {
                    ((CoreBaseFragment) fragment).requestSuccess(response);
                }
            }
        } else {
            if (null != this.activity) {
                if (activity instanceof  CoreBaseController) {
                    ((CoreBaseController) activity).requestSuccess(response);
                }
            }else {
                LogUtil.e("--setResponse--", "["+this.getClass().getSimpleName()+"][setResponse]:" + "Activity not bind for viewmodel " + this.viewModelId + " or the binded activity is not started!");
            }
        }
    }

    /**
     * 根据方法名，回填返回值数据。如果不需要activity，可以在这里处理返回值，例如填UserCenter可放在BaseViewModel中
     *
     * @param object
     */
    public abstract void doPackData(Object object);

    /**
     * called when requestFailed, before activity requestFailedHandler()
     *
     * @param response
     */
    public void preFailedHandle(CoreServiceResponse response){

    }

    public void requestFailed(CoreServiceResponse response) {
        this.response = response;
        isFailed.set(true);
        preFailedHandle(response);
        if (isFragment()){
            if (null != this.fragment){
                if (fragment instanceof CoreBaseFragment) {
                    ((CoreBaseFragment) fragment).requestFailed(response);
                }
            }else{
                LogUtil.e("activity", "["+this.getClass().getSimpleName()+"][requestFailed]:" + "requestFailed,but fragment is null");
            }
        } else {
            if (null != this.activity) {
                ((CoreBaseController) activity).requestFailed(response);
            }else{
                LogUtil.e("activity", "["+this.getClass().getSimpleName()+"][requestFailed]:" + "requestFailed,but activity is null");
            }
        }

    }

    public void setViewModelId(String viewModelId) {
        this.viewModelId = viewModelId;
    }

    public String getViewModelId() {
        return viewModelId;
    }

    public void setActivityClass(String className) {
        activityClassName = className;
    }

    public String getActivityClass() {
        return activityClassName;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setTaskToken(TaskToken token) {
        this.taskToken = token;
    }

    public TaskToken getTaskToken() {
        return this.taskToken;
    }

    public Integer getRequestCode() {
        return requestCode;
    }

    public Fragment getFragment() {
        return fragment;
    }

    public void setFragment(Fragment fragment) {
        this.fragment = fragment;
    }

    public void setRequestCode(int requestCode) {
        this.requestCode = requestCode;
    }

    public String getRequestMethodName() {
        return requestMethodName;
    }

    public void setRequestMethodName (String requestMethodName) {
        this.requestMethodName = requestMethodName;
    }

    public HttpCoreBaseRequest getBaseRequest() {
        return baseRequest;
    }

    public void setBaseRequest(HttpCoreBaseRequest baseRequest) {
        this.baseRequest = baseRequest;
    }

    @Override
    public String toString() {
        return "CoreBaseViewModel{" +
                "viewModelId='" + viewModelId + '\'' +
                ", activityClassName='" + activityClassName + '\'' +
                ", requestCode=" + requestCode +
                ", showProgressBar=" + showProgressBar +
                ", activity=" + activity +
                ", fragment=" + fragment +
                ", isDataReady=" + isDataReady +
                ", isRefreshed=" + isRefreshed +
                ", isFailed=" + isFailed +
                ", requestMethodName='" + requestMethodName + '\'' +
                ", exception=" + exception +
                ", baseRequest=" + baseRequest +
                ", taskToken=" + taskToken +
                ", response=" + response +
                '}';
    }
}
